package com.ctg.behavior.calc.impala.transfer;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtSQLTransfer;
import com.ctg.behavior.calc.impala.report.CtCalcRestrictMeasureReport;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcRestrict;
import com.ctg.behavior.common.calc.CtCalcRestrictMeasure;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.consts.CtFunctionConsts;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtLimitRestrictTransfer implements CtSQLTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtCalcRestrictMeasureReport)) {
			log.warn("calcObject is not CtCalcRestrictMeasureReport, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}
		CtCalcRestrictMeasure restrictMeasure = (CtCalcRestrictMeasure) calcObject;
		log.info("CalcBody={}", JSON.toJSONString(restrictMeasure));

		StringBuilder impalaSQL = new StringBuilder();

		// 1. 计算度量限制
		CtCalcRestrict restrict = restrictMeasure.getRestrict();
		String function = restrict.getFunction();
		if (StringUtils.isEmpty(function) || !function.startsWith("topn_")) {
			return "";
		}
		String property = CtCalcPropertyUtils.getProperty(restrictMeasure.getField());
		// 2. ORDER BY
		if (StringUtils.isEmpty(restrictMeasure.getField())) {
			// 2.1 总次数 -
			// impalaSQL.append(" ORDER BY ").append(restrictMeasure.getEventId());
			String eventId = CtCalcSQLUtils.fillAlias(restrictMeasure.getEventId(), sqlParam);
			impalaSQL.append(" ORDER BY ").append(eventId);
		} else if (CtCalcConsts.GROUPBY_DAY.equalsIgnoreCase(property)) {
			// 2.2 分布 - 天数
			// impalaSQL.append(" ORDER BY event_date ");
			impalaSQL.append(" ORDER BY ");
			impalaSQL.append(CtCalcSQLUtils.fillAlias("event_date", sqlParam));
		} else {
			// 2.3 特定属性
			// impalaSQL.append(" ORDER BY ").append(property);
			impalaSQL.append(" ORDER BY ");
			impalaSQL.append(CtCalcSQLUtils.fillAlias(property, sqlParam));
		}
		// 2.4 升序或降序
		if (CtFunctionConsts.KEY_TOPN_LEAST.equalsIgnoreCase(function)) {
			impalaSQL.append(" ASC ");
			impalaSQL.append(" LIMIT ").append(restrict.getParams().get(0));
		} else if (CtFunctionConsts.KEY_TOPN_MOST.equalsIgnoreCase(function)) {
			impalaSQL.append(" DESC ");
			impalaSQL.append(" LIMIT ").append(restrict.getParams().get(0));
		} else if (CtFunctionConsts.KEY_TOPN_BETWEEN.equalsIgnoreCase(function)) {
			impalaSQL.append(" DESC ");
			impalaSQL.append(" LIMIT ").append(restrict.getParams().get(0));
			impalaSQL.append(" , ").append(restrict.getParams().get(1));
		}

		String sqlCondtions = impalaSQL.toString();
		log.info("CalcSQL={}", sqlCondtions);

		return sqlCondtions;
	}

}
